<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商区名称" prop="blockId">
        <el-select v-model="queryParams.blockId" placeholder="请选择商业区名" clearable @change="handleQuery">
          <el-option v-for="item in blockList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="楼栋名称" prop="buildId">
        <el-select v-model="queryParams.buildId" placeholder="请选择楼栋名" clearable @change="handleQuery">
          <el-option v-for="item in buildingList" :key="item.id" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商铺编号" prop="id">
        <el-input v-model="queryParams.id" placeholder="请输入商铺编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable @change="handleQuery">
          <el-option v-for="dict in dict.type.sys_shop_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:shops:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:shops:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:shops:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:shops:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商铺编号" align="center" prop="id" />
      <el-table-column label="商区名称" align="center" prop="blockName" />
      <el-table-column label="楼栋名称" align="center" prop="buildingName" />
      <el-table-column label="楼层" align="center" prop="floor" />
      <el-table-column label="房号" align="center" prop="roomNum" />
      <el-table-column label="占地面积" align="center" prop="allArea" />
      <el-table-column label="实际面积" align="center" prop="realArea" />
      <el-table-column label="租金/售价" align="center" prop="money" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_shop_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:shops:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:shops:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改商铺管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">

        <!-- <el-form-item label="楼栋" prop="blockId">
          <el-cascader
              v-model="value"
              :options="treeList.name"
              @change="handleChange"></el-cascader>
        </el-form-item> -->
        <el-form-item label="商区名称" prop="blockId">
          <el-select :disabled="form.id" v-model="params.block" placeholder="请选择商业区名" clearable
            @change="getBuildingList()">
            <el-option v-for="item in blockList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼栋名称" prop="buildId">
          <el-select :disabled="form.id" v-model="form.buildId" placeholder="请选择楼栋名" clearable>
            <el-option v-for="item in buildingList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入楼层" />
        </el-form-item>
        <el-form-item label="房号" prop="roomNum">
          <el-input v-model="form.roomNum" placeholder="请输入房号" />
        </el-form-item>
        <el-form-item label="占地面积" prop="allArea">
          <el-input v-model="form.allArea" placeholder="请输入占地面积" />
        </el-form-item>
        <el-form-item label="实际面积" prop="realArea">
          <el-input v-model="form.realArea" placeholder="请输入实际面积" />
        </el-form-item>
        <el-form-item label="状态" prop="status" v-if="form.status">
          <el-input v-model="shopStatus[form.status]" :disabled="true" />
        </el-form-item>
        <el-form-item label="租金/售价" prop="money">
          <el-input v-model="form.money" placeholder="请输入租金/售价" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listShops,
    getShops,
    delShops,
    addShops,
    updateShops
  } from "@/api/system/shops";
  import {
    listBlock,
    treeList
  } from "@/api/system/block";
  import {
    listBuilding
  } from "@/api/system/building";

  export default {
    name: "Shops",
    dicts: ['sys_shop_status'],
    data() {
      return {
        value: [],
        // 状态判断字典
        shopStatus: {},
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 商铺管理表格数据
        shopsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          id: null,
          blockId: null,
          buildId: null,
          status: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          status: [{
            required: true,
            message: "状态不能为空",
            trigger: "change"
          }]
        },
        // 商业区列表
        blockList: [],
        //
        params: {
          block: null,
        },
        // 楼栋列表
        buildingList: [],
        //商业区和楼栋的树形数据
        treeList: []
      };
    },
    created() {
      //获取字典数据
      this.fetchDict();
      this.getList();
      this.getBlockList();
      this.getBuildingList();
      //获取树形数据
      // this.getTreeList();

    },
    methods: {
      /** 获取树形数据 */
      getTreeList() {
        treeList().then(response => {
          this.treeList = response.rows
        })
      },
      //获取自定义字典数据
      fetchDict() {
        this.shopStatus = {
          "1": "已出租",
          "2": "已出售",
          "3": "空置"
        };
      },
      /** 查询商业区列表 */
      getBlockList() {
        listBlock(null).then(response => {
          this.blockList = response.rows;
        })
      },
      /** 查询楼栋列表 */
      getBuildingList() {
        this.form.blockId = this.params.block
        listBuilding(this.params).then(response => {
          this.buildingList = response.rows;
        })
      },

      /** 查询商铺管理列表 */
      getList() {
        this.loading = true;
        listShops(this.queryParams).then(response => {
          this.shopsList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          blockId: null,
          buildId: null,
          floor: null,
          roomNum: null,
          allArea: null,
          realArea: null,
          money: null,
          status: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.params.block = this.queryParams.blockId;
        this.getBuildingList();
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.params.block = null;
        this.open = true;
        this.title = "添加商铺管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getShops(id).then(response => {
          this.form = response.data;
          this.params.block = response.data.blockId;
          this.open = true;
          this.title = "修改商铺管理";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateShops(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addShops(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除商铺管理编号为"' + ids + '"的数据项？').then(function() {
          return delShops(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/shops/export', {
          ...this.queryParams
        }, `shops_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
